import fetch from '../config/fetch'
import { Loading } from 'element-ui';
import * as config from '../config/const';

export const ADMIN_JSON='/adminjson';

export const userinfo = () => fetch(ADMIN_JSON+'/userinfo.json');
export const logout = () => fetch(ADMIN_JSON+'/logout.json');
export const lgabh = (param) => fetch(ADMIN_JSON+'/lgabh.json',param);
//图片上传获取秘钥
export const imgAccessId = (param) => fetch('/wapsjson/imgAccessId.json',param);
//商品
export const allitemList = (param) => fetch(ADMIN_JSON+'/allitemList.json',param);
export const itemCodeToId = (param) => fetch(ADMIN_JSON+'/itemCodeToId.json',param);
export const deleteItem = (param) => fetch(ADMIN_JSON+'/deleteItem.json',param);
export const noSaleItem = (param) => fetch(ADMIN_JSON+'/noSaleItem.json',param);
export const sellers = (param) => fetch(ADMIN_JSON+'/sellers.json',param);
export const getMainCateList = (param) => fetch(ADMIN_JSON+'/getMainCateList.json',param);
export const getChildCateList = (param) => fetch(ADMIN_JSON+'/getChildCateList.json',param);
export const addItem = (param) => fetch(ADMIN_JSON+'/addItem.json',param,'POST');
export const selItemById = (param) => fetch(ADMIN_JSON+'/selItemById.json',param);
export const saleItem = (param) => fetch(ADMIN_JSON+'/saleItem.json',param);
export const modifyItem = (param) => fetch(ADMIN_JSON+'/modifyItem.json',param,'POST');
export const modifyItemAdvert = (param) => fetch(ADMIN_JSON+'/modifyItemAdvert.json',param);
export const getShiguGoods = (param) => fetch(ADMIN_JSON+'/getShiguGoods.json',param);
export const getItemDaifaInfo = (param) => fetch(ADMIN_JSON+'/getItemDaifaInfo.json',param);
//商品类目
export const selMainCate = (param) => fetch(ADMIN_JSON+'/selMainCate.json',param);
export const addCate = (param) => fetch(ADMIN_JSON+'/addCate.json',param);
export const deleteCate = (param) => fetch(ADMIN_JSON+'/deleteCate.json',param);
export const modifyCate = (param) => fetch(ADMIN_JSON+'/modifyCate.json',param);
//订单管理
export const toSendList = (param) => fetch(ADMIN_JSON+'/toSendList.json',param);
export const allOrderList = (param) => fetch(ADMIN_JSON+'/allOrderList.json',param);
export const orderSend = (param) => fetch(ADMIN_JSON+'/orderSend.json',param);
export const closeTrade = (param) => fetch(ADMIN_JSON+'/closeTrade.json',param);
export const remarkOrder = (param) => fetch(ADMIN_JSON+'/remarkOrder.json',param);
export const sendedList = (param) => fetch(ADMIN_JSON+'/sendedList.json',param);
export const refundTrade = (param) => fetch(ADMIN_JSON+'/refundTrade.json',param);
export const changeTradeReceive = (param) => fetch(ADMIN_JSON+'/changeTradeReceive.json',param);
export const tradeBonus = (param) => fetch(ADMIN_JSON+'/tradeBonus.json',param);
export const queryWxRefund = (param) => fetch(ADMIN_JSON+'/queryWxRefund.json',param);
//广告
export const advertList = (param) => fetch(ADMIN_JSON+'/advertList.json',param);
export const addAdvert = (param) => fetch(ADMIN_JSON+'/addAdvert.json',param);
export const deleteAdvert = (param) => fetch(ADMIN_JSON+'/deleteAdvert.json',param);
export const modifyAdvert = (param) => fetch(ADMIN_JSON+'/modifyAdvert.json',param);
//热销商品
export const hotItemList = (param) => fetch(ADMIN_JSON+'/hotItemList.json',param);
export const addHotItem = (param) => fetch(ADMIN_JSON+'/addHotItem.json',param);
export const delHotItem = (param) => fetch(ADMIN_JSON+'/delHotItem.json',param);
export const modifyHotItem = (param) => fetch(ADMIN_JSON+'/modifyHotItem.json',param);
//商家
export const sellerList = (param) => fetch(ADMIN_JSON+'/sellerList.json',param);
export const addSeller = (param) => fetch(ADMIN_JSON+'/addSeller.json',param);
export const modifySellerName = (param) => fetch(ADMIN_JSON+'/modifySellerName.json',param);
export const deleteSeller = (param) => fetch(ADMIN_JSON+'/deleteSeller.json',param);
//运费模板
export const addPostFeeModel = (param) => fetch(ADMIN_JSON+'/addPostFeeModel.json',param);
export const postModelList = () => fetch(ADMIN_JSON+'/postModelList.json');
export const modifyPostFeeModel = (param) => fetch(ADMIN_JSON+'/modifyPostFeeModel.json',param);
export const deletePostModel = (param) => fetch(ADMIN_JSON+'/deletePostModel.json',param);
export const postFeeModel = (param) => fetch(ADMIN_JSON+'/postFeeModel.json',param);
//客服信息
export const getContactInfo = (param) => fetch(ADMIN_JSON+'/getContactInfo.json',param);
export const modifyContact = (param) => fetch(ADMIN_JSON+'/modifyContact.json',param);
export const modifyAskList = (param) => fetch(ADMIN_JSON+'/modifyAskList.json',param,'POST');
//常量
export const myconstants = (param) => fetch(ADMIN_JSON+'/myconstants.json',param);
export const delConstants = (param) => fetch(ADMIN_JSON+'/delConstants.json',param);
export const addConstants = (param) => fetch(ADMIN_JSON+'/addConstants.json',param,'POST');
export const modifyConstants = (param) => fetch(ADMIN_JSON+'/modifyConstants.json',param,'POST');
export const synConstants = (param) => fetch(ADMIN_JSON+'/synConstants.json',param);
export const uploadApk = (param) => fetch(ADMIN_JSON+'/uploadApk.json',param);
//用户列表页面
export const mallUserList = (param) => fetch(ADMIN_JSON+'/mallUserList.json',param);
export const moneyRecord = (param) => fetch(ADMIN_JSON+'/moneyRecord.json',param);
export const changeUserLevel = (param) => fetch(ADMIN_JSON+'/changeUserLevel.json',param);
//提现
export const waitCashList = (param) => fetch(ADMIN_JSON+'/waitCashList.json',param);
export const examineCash = (param) => fetch(ADMIN_JSON+'/examineCash.json',param);
export const cashRecord = (param) => fetch(ADMIN_JSON+'/cashRecord.json',param);
export const rewardUser = (param) => fetch(ADMIN_JSON+'/rewardUser.json',param);
export const addUserVoucher = (param) => fetch(ADMIN_JSON+'/addUserVoucher.json',param);

//手机端版本常量
export const getVersionConstant = (param) => fetch(ADMIN_JSON+'/getVersionConstant.json',param);
export const selOsConstant = (param) => fetch(ADMIN_JSON+'/selOsConstant.json',param);

//首页热搜词设置
export const getIndexHotSearchWord = (param) => fetch(ADMIN_JSON+'/getIndexHotSearchWord.json',param);
export const setIndexHotSearchWord = (param) => fetch(ADMIN_JSON+'/setIndexHotSearchWord.json',param);
export const deleteIndexHotSearchWord = (param) => fetch(ADMIN_JSON+'/deleteIndexHotSearchWord.json',param);

//显示抢购设置
export const getLimitGoodsList = (param) => fetch(ADMIN_JSON+'/getLimitGoodsList.json',param);
export const setLimitGoods = (param) => fetch(ADMIN_JSON+'/setLimitGoods.json',param);
export const modifyLimitGoods = (param) => fetch(ADMIN_JSON+'/modifyLimitGoods.json',param);
export const deleteLimitGoods = (param) => fetch(ADMIN_JSON+'/deleteLimitGoods.json',param);

//显示抢购设置
export const getActivityList = (param) => fetch(ADMIN_JSON+'/getActivityList.json',param);
export const setActivityItem = (param) => fetch(ADMIN_JSON+'/setActivityItem.json',param);
export const deleteActivityItem = (param) => fetch(ADMIN_JSON+'/deleteActivityItem.json',param);

//每日报表接口
export const dailyReportList = (param) => fetch(ADMIN_JSON+'/dailyReportList.json',param);
export const tradeReport = (param) => fetch(ADMIN_JSON+'/tradeReport.json',param);
export const  vipReport = (param) => fetch(ADMIN_JSON+'/vipReport.json',param);

export const ajax = {
    data(){
      return {
        loadingTool:'',
        _imdoing:''
      }
    },
    methods: {
        async doajax(method,param,success,err){
          if (this._imdoing==method) {
            return;
          }
          this._imdoing=method;
          this.openLoading();
          this.ajax(await method(param),success,err);
          this.closeLoading();
          this._imdoing='';
        },
        ajax(res, success,err){
            if(res.success){
                success.call(this, res)
            }else{
              if(err!==undefined){
                    err.call(this, res);
                }else if(res.auth!==undefined){
                   var filter=config.AUTH_FILTER.admin;
                   this.$router.push(filter.loginUrl);
                 }else{
                    this.error(res.msg);
                }
            }

        },
      openLoading(){
        this.loadingTool=Loading.service({ fullscreen: true });
      },
      closeLoading(){
        this.loadingTool.close();
      },
      success(message){
        this.$message({
          message: message,
          type: 'success'
        });
      },
      error(message){
        this.$message.error(message);
      },
      warn(message){
        this.$message({
          message: message,
          type: 'warning'
        });
      }
    }
};
export const session={
  data(){
    return {}
  },
  mixins:[ajax],
  methods:{
    removeUser(){
      sessionStorage.removeItem(config.SESSION_KEY);
    },
    async getLoginUser(){
      //得到sessionStorage里的用户数据
      var user=sessionStorage.getItem(config.SESSION_KEY);
      if(user && user.indexOf('autostringify-') === 0 ){//有取到用户数据
        return JSON.parse(user.split('autostringify-')[1]);
      }else{//没取到直接用户数据，尝试去后台取
        //请求后端得到user
        await this.doajax(userinfo,{},(resp) => {
          sessionStorage.setItem(config.SESSION_KEY,'autostringify-'+JSON.stringify(resp.info));
          user=resp.info;
        });
        return user;
      }
    }
  }
};
